/*
 * Created on Sep 28, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for a period.
 */
public class PeriodContainer extends Container {

	// The map of timer buckets for all databases
	protected static Hashtable dbStores = new Hashtable();
	// The name of the period id of the course
	private StringItem periodName =
		new StringItem(SchoolDBSQL.PERIODNAME, "", false);
	// A list of the hours the period occurs
	private ArrayList hours = new ArrayList(5);

	// Default constructor
	private PeriodContainer() {

	}

	/**
	 * Constructor creates a period container with values loaded from the database
	 * using the name passed in as a parameter.
	 * @param newName The name of the period to load.
	 * @param dbName The name of the school database
	 */
	private PeriodContainer(String newName, String dbName) {
		getPeriodName().setValue(newName);
		SchoolDBSQL.getSchoolDBSQL(dbName).loadPeriod(this);
	}

	public static PeriodContainer getPeriodContainer(String newName, String dbName) {
		PeriodContainer returnVal = null;
		DBStore dbs = null;

		if ("".equals(newName)) {
			returnVal = new PeriodContainer();
		}
		else {
			if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
				dbs = new DBStore();
				dbStores.put(dbName, dbs);
				dbs.start();
			}
			if ((returnVal = (PeriodContainer) dbs.getContainer(newName)) == null) {
				returnVal = new PeriodContainer();
				returnVal.getPeriodName().setValue(newName);
				SchoolDBSQL.getSchoolDBSQL(dbName).loadPeriod(returnVal);
				dbs.addContainer(returnVal, newName);
			}
		}

		return returnVal;
	}

	/* (non-Javadoc)
	 * @see schoolsource.model.Container#getValidationItems()
	 */
	public Item[] getValidationItems() {
		ArrayList al = new ArrayList();
		al.add(periodName);

		return (Item[]) al.toArray(new Item[al.size()]);
	}

	/**
	 * Getter for the name of the period.
	 * @return The name of the period.
	 */
	public StringItem getPeriodName() {
		return periodName;
	}

	/**
	 * Getter for the short name of the course.
	 * @return The short name of the course.
	 */
	public HourContainer[] getHours() {
		return (HourContainer[]) hours.toArray(new Container[hours.size()]);
	}

	/**
	 *
	 *
	 */
	 public void addHour(HourContainer hc) {
		 hours.add(hc);
	 }

	 /**
	  *
	  *
	  */
	  public void removeHour(HourContainer hc) {
		  if (hc != null) {
			  for (int i = 0; i < hours.size(); i++) {
				  if (((HourContainer) hours.get(i)).getDayOfWeek().toString().equals(hc.getDayOfWeek().toString()) &&
				  	  ((HourContainer) hours.get(i)).getPeriodStartTime().toString().equals(hc.getPeriodStartTime().toString()) &&
				  	  ((HourContainer) hours.get(i)).getPeriodEndTime().toString().equals(hc.getPeriodEndTime().toString())) {
					  hours.remove(i);
				  }
			  }
		  }
	  }

	/**
	 * Prints the values of the container.
	 */
	public void printItems() {
		SchoolLogger.logger.log(
			Level.INFO,
			"************************************************");
		Item iList[] = getValidationItems();
		for (int j = 0; j < iList.length; j++) {
			if (iList[j] != null) {
				SchoolLogger.logger.log(
					Level.INFO,
					iList[j].getFieldName() + ": " + iList[j].toString());
			}
		}
	}

}
